using System;
using svs.Mobile.BeyondMedia.UiCore.GDI;

namespace svs.Mobile.BeyondMedia.UiCore.GDI
{
	public class BrushGdi : IDisposable
	{
		protected IntPtr	m_hbrush;
		private bool		m_stock = false;

		public static BrushGdi Empty = new BrushGdi(IntPtr.Zero);

		protected BrushGdi()
		{
		}

		public BrushGdi(IntPtr hbrush)
		{
			m_hbrush = hbrush;
		}

		public BrushGdi(IntPtr hbrush, bool stock)
		{
			m_hbrush = hbrush;
			m_stock = stock;
		}

		~BrushGdi()
		{
			Release();
		}

		public void Dispose()
		{
			Release();
			GC.SuppressFinalize(this);
		}

		public static explicit operator IntPtr(BrushGdi brush)
		{
			if (brush == null)
				return IntPtr.Zero;
			else
				return brush.m_hbrush;
		}

		public static implicit operator BrushGdi(IntPtr hbrush)
		{
			return new BrushGdi(hbrush);
		}

		private void Release()
		{
			if (m_hbrush != IntPtr.Zero)
			{
				if (!m_stock)
					Win32.DeleteObject(m_hbrush);
				m_hbrush = IntPtr.Zero;
			}
		}
	}
}